package com.sc.nft.dao;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.enums.ApprovalStatusEnum;
import com.sc.nft.enums.MarketLevelEnum;
import com.sc.nft.enums.wallet.DepartmentDetailTypeEnum;
import com.sc.nft.enums.wallet.DepartmentTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.DepartmentWalletDetail;
import com.sc.nft.mapper.DepartmentWalletDetailMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * 部门钱包明细表(sc_department_wallet_detail)数据DAO
 *
 * @author zxy
 * @since 2023-12-18 18:02:52
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class DepartmentWalletDetailDao extends ServiceImpl<DepartmentWalletDetailMapper, DepartmentWalletDetail> {

    public Page<DepartmentWalletDetail> record(DepartmentTypeEnum departmentType,Long userId, DepartmentDetailTypeEnum type, MarketLevelEnum marketLevel, ApprovalStatusEnum status, String orderNo, Date startTime, Date endTime, Page page){
        LambdaQueryWrapper<DepartmentWalletDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(userId!=null,DepartmentWalletDetail::getUserId,userId);
        wrapper.eq(departmentType!=null,DepartmentWalletDetail::getDepartmentType,departmentType);
        wrapper.eq(status!=null,DepartmentWalletDetail::getStatus,status);
        wrapper.eq(type!=null,DepartmentWalletDetail::getType,type);
        wrapper.eq(marketLevel!=null,DepartmentWalletDetail::getMarketLevel,marketLevel);
        wrapper.eq(StrUtil.isNotBlank(orderNo),DepartmentWalletDetail::getOrderNo,orderNo);
        wrapper.ge(startTime!=null,DepartmentWalletDetail::getCreateTime,startTime);
        wrapper.le(endTime!=null,DepartmentWalletDetail::getCreateTime,endTime);
        wrapper.orderByDesc(DepartmentWalletDetail::getId);
        return page(page,wrapper);
    }


    public void updateApprovalStatus(Long id,ApprovalStatusEnum status){
        LambdaUpdateWrapper<DepartmentWalletDetail> wrapper =Wrappers.lambdaUpdate();
        wrapper.set(DepartmentWalletDetail::getStatus,status);
        wrapper.eq(DepartmentWalletDetail::getId,id);
        Assert.isTrue(update(wrapper),()->new GlobalRunTimeException("数据已改变，请刷新后重试"));
    }
}